import { useEffect, FC, ReactNode } from 'react';
import { Provider } from 'react-redux';
import dva from './dva/index';
import models from './models';
import 'taro-ui/dist/style/index.scss';
import './app.less';
import { checkLogin } from './untils/auth'
import Taro from '@tarojs/taro';
import { getUserInfo } from '@api/login.ts'

const dvaApp = dva.createApp({
  initialState: {},
  models: models,
});
const store = dvaApp.getStore();

// wx.cloud.init({
//   env: 'wx5bdfc4d052cbe381', // 填写云开发环境ID
//   traceUser: true,    // 是否在用户访问时记录用户
// });

// 定义 Props 接口
interface Props {
  children: ReactNode;
}

const App: FC<Props> = ({ children }) => {

  const getMyUserInfo = async () => {
    const result = await getUserInfo()
    if (result.code === 200) {
      const data = result.data
      Taro.setStorageSync('myuserInfo', data)
    }
  }
  useEffect(() => {
    const isLoggedIn = checkLogin();
    if (!isLoggedIn) {
      Taro.navigateTo({ url: '/pages/login/index' });
      return
    }
    const isHaveMyInfo = Taro.getStorageSync('myuserInfo')
    if (!isHaveMyInfo) {
      getMyUserInfo()
    }
  }, []);

  return (
    <Provider store={store}>
      <view className='body'>
        {children}
      </view>
    </Provider>

  );
};

export default App;
